package com.boc.langxiang.presenter.me;

import com.boc.langxiang.contract.me.ConcernsActivityContract;
import com.boc.langxiang.entity.request.ConcernCancelRequest;
import com.boc.langxiang.entity.request.ConcernListRequest;
import com.boc.langxiang.entity.response.ArticleEntity;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.ConcernListContent;
import com.boc.langxiang.entity.response.ConcernsCancelContent;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by admin on 2016/6/27.
 */
public class ConcernsActivityPresenter implements ConcernsActivityContract.Presenter {

    private final ApiService mService;
    private ConcernsActivityContract.View mView;

    public ConcernsActivityPresenter(ConcernsActivityContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;

    }

    @Override
    public void concernList(String page) {
        mView.showLoading();
        ConcernListRequest concernListRequest = new ConcernListRequest();
        concernListRequest.page = page;
        final Observable<BaseResponse<ConcernListContent>> concernList = mService.concernList(concernListRequest.params());
        concernList.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ConcernListContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ConcernListContent> response) {
                        ConcernListContent content = response.getContent();
                        if (content != null) {
                            List<ArticleEntity> list = content.getList();
                            mView.getConcernList(list);
                        } else {
                            mView.getConcernList(null);
                        }

                    }
                });
    }

    @Override
    public void delConcernlist(String id, final int layoutPosition) {
        ConcernCancelRequest concernCancelRequest = new ConcernCancelRequest();
        concernCancelRequest.id = id;
        Observable<BaseResponse<ConcernsCancelContent>> delConcernlist = mService.delConcernlist(concernCancelRequest.params());
        delConcernlist.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ConcernsCancelContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ConcernsCancelContent> response) {
                        mView.freshRequest(layoutPosition);
                    }
                });
    }
}
